package cn.ffcs.uec.outStaff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.component.bean.UecOutPsndocWadocListboxBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.manager.UecPsndocWadocManager;
import cn.ffcs.uec.staff.model.UecPsndocWadoc;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * 外包人员薪资变动情况
 * 
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年9月27日
 */
@Controller
@Scope("prototype")
public class UecOutPsndocWadocListboxExt extends Div implements IdSpace {

    private static final long serialVersionUID = 4408120952122215580L;

    private UecOutPsndocWadocListboxBean bean = new UecOutPsndocWadocListboxBean();

    private static final String UEC_OUTSTAFF_PSNDOC_WADOC_LISTBOX_ZUL = "/pages_nmg/uec_outStaff/comp/uec_outStaff_psndoc_wadoc_listbox_ext.zul";

    private static final String UEC_OUTSTAFF_PSNDOC_WADOC_EDIT_ZUL = "/pages_nmg/uec_outStaff/comp/uec_outStaff_psndoc_wadoc_edit.zul";

    private UecPsndocWadocManager uecPsndocWadocManager = (UecPsndocWadocManager) ApplicationContextUtil
            .getBean("uecPsndocWadocManager");

    /**
     * 外包人员基本信息
     */
    private UecOutStaffInfo uecOutStaffInfo;

    /**
     * 薪资变动情况
     */
    private UecPsndocWadoc uecPsndocWadoc;

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    public UecOutPsndocWadocListboxExt() {
        Executions.createComponents(UEC_OUTSTAFF_PSNDOC_WADOC_LISTBOX_ZUL, this, null);
        Components.wireVariables(this, bean);
        Components.addForwards(this, this, '$');
        
        this.addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNDOC_WADOC_QUERY, this,
                UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNDOC_WADOC_QUERY_RESPONSE);
        
        this.addForward(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PSNWADOC, this,
                UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PSNWADOC_RESPONSE);
    }

    /**
     * 初始化
     */
    public void onCreate() {
        this.setButtonValid(false, false, false, false);
    }

    /**
     * 接收UecOutStaffMainComposer类传过来的outStaffInfo事件
     * 
     * @param event
     */
    public void onUecOutPsndocWodocQueryResponse(ForwardEvent event) {
        uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
        if (null != uecOutStaffInfo) {
            //设置操作按钮，非盟市主管不可操作按钮
            if(PermissionUtil.isNoMainOutStaff()) {
                this.setButtonValid(false, false, false, false);
            }else {
                this.setButtonValid(true, true, true, false);
            }
            uecPsndocWadoc = new UecPsndocWadoc();
            uecPsndocWadoc.setStaffId(uecOutStaffInfo.getOutStaffId());
            uecPsndocWadoc.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
            uecPsndocWadoc.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
            this.onQueryUecOutPsndocWadocList();
        }
    }

    /**
     * 清除变量事件
     * 
     * @param event
     */
    public void onCleanUecOutStaffPsnWadocResponse(ForwardEvent event) {
        clearVariable();
        uecOutStaffInfo = null;
        ListboxUtils.clearListbox(this.bean.getUecOutPsndocWadocListbox());
        this.bean.getUecOutPsndocWadocListboxPaging().setTotalSize(1);
    }

    /**
     * 选择查询记录/选中事件
     * 
     * @throws Exception
     */
    public void onSelectUecOutPsndocWadocList() throws Exception {
        if (this.bean.getUecOutPsndocWadocListbox().getSelectedCount() > 0) {
            uecPsndocWadoc = (UecPsndocWadoc) bean.getUecOutPsndocWadocListbox().getSelectedItem().getValue();
        }
    }

    /**
     * 新增按钮
     * 
     * @throws Exception @throws
     */
    public void onAddUecOutPsndocWadoc() throws Exception {
        if (null != uecOutStaffInfo) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("opType", "add");
            map.put("uecOutStaffInfo", uecOutStaffInfo);
            Window window = (Window) Executions.createComponents(UEC_OUTSTAFF_PSNDOC_WADOC_EDIT_ZUL, this, map);
            window.doModal();
            window.addEventListener("onOK", new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (event.getData() != null) {
                        // 新增或修改完成后，列表从数据库更新
                        uecPsndocWadoc = (UecPsndocWadoc) event.getData();
                        onQueryUecOutPsndocWadocList();
                    }
                }
            });
        }
    }

    /**
     * 编辑按钮
     * 
     * @throws Exception
     * @throws InterruptedException
     */
    public void onEditUecOutPsndocWadoc() throws Exception, InterruptedException {
        if (null != uecPsndocWadoc && uecPsndocWadoc.getId() != null) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("opType", "edit");
            map.put("uecPsndocWadoc", uecPsndocWadoc);
            map.put("uecOutStaffInfo", uecOutStaffInfo);
            Window window = (Window) Executions.createComponents(UEC_OUTSTAFF_PSNDOC_WADOC_EDIT_ZUL, this, map);
            window.doModal();
            window.addEventListener("onOK", new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (event.getData() != null) {
                        // 新增或修改完成后，列表从数据库更新
                        uecPsndocWadoc = (UecPsndocWadoc) event.getData();
                        onQueryUecOutPsndocWadocList();
                    }
                }
            });
        } else {
            ZkUtil.showError("请选择要编辑的记录", "提示信息");
            return;
        }
    }

    /**
     * 查询
     */
    public void onQueryUecOutPsndocWadocList() {
        if (null != uecPsndocWadoc) {
            PageInfo pageInfo = uecPsndocWadocManager.queryPageInfoByUecPsndocWadoc(uecPsndocWadoc,
                    this.bean.getUecOutPsndocWadocListboxPaging().getActivePage() + 1,
                    this.bean.getUecOutPsndocWadocListboxPaging().getPageSize());
            ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
            this.bean.getUecOutPsndocWadocListbox().setModel(list);
            this.bean.getUecOutPsndocWadocListboxPaging().setTotalSize(pageInfo.getTotalCount());
        }
    }

    /**
     * 翻页
     */
    public void onUecOutPsndocWadocListboxPaging() {
        this.onQueryUecOutPsndocWadocList();
    }

    /**
     * 清除变量
     */
    public void clearVariable() {
        uecPsndocWadoc = null;
    }

    /**
     * 设置按钮
     * 
     * @param canAdd
     * @param candel
     */
    private void setButtonValid(boolean canView, boolean canAdd, boolean canEdit, boolean candel) {
        this.bean.getViewButton().setDisabled(!canView);
        this.bean.getAddButton().setDisabled(!canAdd);
        this.bean.getEditButton().setDisabled(!canEdit);
        this.bean.getDelButton().setDisabled(!candel);
    }
}
